﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;

using JigsawService.Parsers;
using JigsawService.Utility;
using JigsawService.DataAccess.Security;

namespace JigsawService.Controllers
{
    public class Testing : JigsawBase
    {
        APIMessage _apimessage;

        public Testing(APIMessage apiMessage)
        {
            _apimessage = apiMessage;
        }

        /// <summary>
        /// The Testing controller should be limited to admin users, just like the SystemAdmin controller
        /// </summary>
        public void Controller()
        {
            _apimessage.StateMessage = "Testing Controller: ";
            
            // method access authorization
            if (_apimessage.ValidateMethodAccess(_apimessage.APIString))
            {
                switch (_apimessage.APIString)
                {
                    case "Testing.EchoData.Base":
                        EchoData();
                        break;

                    case "Testing.GetUserMethods.Base":
                        GetUserMethods();
                        break;

                }
            }
            else
            {
                throw new Exception("Method Authorization Failed for " + _apimessage.APIString);
            }
        }

        // ****************************************
        // ****************************************


        /// <summary>
        /// Echos the input parameters as the response message results;
        /// used to test connections to a given service.
        /// </summary>
        protected void EchoData()
        {
            _apimessage.StateMessage = "Testing EchoData: ";
            string echoText = _apimessage.ValidateMethodParam("EchoText");
            SetSuccessResult(echoText, "text", _apimessage);
        }

        /// <summary>
        /// This method is used to retrieve a list of the methods accessible to a user by action type,
        /// used to test the methods in the test app.
        /// </summary>
        protected void GetUserMethods()
        {
            _apimessage.StateMessage = "Testing GetUserMethods: ";
            string userid = _apimessage.ValidateMethodParam("UserID");

            if (userid != "")
            {
                MethodsDA methda = new MethodsDA(_apimessage);
                methda.GetUserMethods(userid);
            }
            else
            {
                throw new Exception("Missing UserID input parameter.");
            }
        }

    }
}